<?php

/**
 * 订单列表
 */

namespace app\shop\common;

class OrderCommon extends \app\member\common\MemberCommon {


    protected function index() {
        $this->pageInfo['name'] = '我的订单';
        $this->setMeta('我的订单');
        $this->setCrumb([
            [
                'name' => '我的订单',
                'url' => url()
            ]
        ]);
        $urlParam = [];
        $where = [];
        $type = request('get', 'type', 0, 'intval');
        if ($type) {
            $where['B.order_status'] = 1;
        }
        $urlParam['type'] = $type;
        switch ($type) {
            case 1:
                $where['B.pay_type'] = 1;
                $where['B.pay_status'] = 0;
                $where['delivery_status'] = 0;
                break;
            case 2:
                $where['_sql'][] = '(B.pay_type = 0 OR B.pay_status = 1)';
                $where['delivery_status'] = 0;
                break;
            case 3:
                $where['delivery_status'] = 1;
                $where['order_complete_status'] = 0;
                break;
            case 4:
                $where['order_complete_status'] = 1;
                $where['comment_status'] = 0;
                break;
        }

        $pageLimit = 10;
        $where['B.order_user_id'] = $this->userInfo['user_id'];

        $model = target('shop/ShopOrder');
        $count = $model->countList($where);
        $pageData = $this->pageData($count, $pageLimit, $urlParam);
        $list = $model->loadList($where, $pageData['limit'], 'B.order_id desc');

        $orderIds = [];
        foreach ($list as $key => $vo) {
            $orderIds[] = $vo['order_id'];
        }
        if (!empty($orderIds)) {
            $orderGoods = target('order/OrderGoods')->loadList([
                '_sql' => 'order_id in (' . implode(',', $orderIds) . ')'
            ]);
            $orderGroup = [];
            foreach ($orderGoods as $key => $vo) {
                $orderGroup[$vo['order_id']][] = $vo;
            }
            foreach ($list as $key => $vo) {
                $list[$key]['order_items'] = $orderGroup[$vo['order_id']];
            }
        }

        $this->assign('page', $pageData['html']);
        $this->assign('pageList', $list);
        $this->assign('pageParams', $urlParam);
        $this->assign('type', $type);
        $this->memberDisplay();
    }

    protected function info() {
        $orderNo = request('get', 'order_no', 0);
        if (empty($orderNo)) {
            $this->error404();
        }
        $orderInfo = target('shop/ShopOrder')->getWhereInfo([
            'B.order_no' => $orderNo
        ]);
        if (empty($orderInfo)) {
            $this->error404();
        }
        if ($orderInfo['order_user_id'] <> $this->userInfo['user_id']) {
            $this->error404();
        }
        $this->pageInfo['name'] = '订单详情';
        $this->setMeta('订单详情');
        $this->setCrumb([
            [
                'name' => '订单详情',
                'url' => url()
            ]
        ]);
        $payInfo = [];
        if ($orderInfo['pay_status']) {
            $payInfo = target('member/PayLog')->getInfo($orderInfo['pay_id']);
        }
        $deliveryInfo = [];
        if ($orderInfo['delivery_status']) {
            $deliveryInfo = target('order/OrderDelivery')->getWhereInfo([
                'A.order_id' => $orderInfo['order_id']
            ]);
        }

        $step = 1;
        if ($orderInfo['pay_type']) {
            if ($orderInfo['status_data']['action'] == 'pay') {
                $step = 1;
            }
            if ($orderInfo['status_data']['action'] == 'delivery' || $orderInfo['status_data']['action'] == 'parcel') {
                $step = 2;
            }
            if ($orderInfo['status_data']['action'] == 'receive') {
                $step = 3;
            }
            if ($orderInfo['status_data']['action'] == 'complete'|| $orderInfo['status_data']['action'] == 'comment') {
                $step = 4;
            }
        }else {
            if ($orderInfo['status_data']['action'] == 'delivery' || $orderInfo['status_data']['action'] == 'parcel') {
                $step = 1;
            }
            if ($orderInfo['status_data']['action'] == 'receive') {
                $step = 2;
            }
            if ($orderInfo['status_data']['action'] == 'pay') {
                $step = 3;
            }
            if ($orderInfo['status_data']['action'] == 'complete' || $orderInfo['status_data']['action'] == 'comment') {
                $step = 4;
            }
        }

        $orderGoods = target('order/OrderGoods')->loadList([
            'order_id' => $orderInfo['order_id']
        ]);

        $logList = target('order/OrderLog')->loadList(['order_id' => $orderInfo['order_id'], 'type' => 1]);
        $this->assign('payInfo', $payInfo);
        $this->assign('deliveryInfo', $deliveryInfo);
        $this->assign('info', $orderInfo);
        $this->assign('orderGoods', $orderGoods);
        $this->assign('orderStep', $step);
        $this->assign('logList', $logList);
        $this->memberDisplay();


    }

}